package com.pic.mgt.haishin.converter;

import java.util.List;

import com.pic.mgt.haishin.unit.AppointedHaishinBusinessUnit;
import com.pic.mgt.haishin.unit.HaishinBusinessUnit;
import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.monitor.parser.FormatParser;

/**
 * [指示配信確認檔內容處理]
 * 處理項目: * 
 * 1. 端末清單:確認檔內容的第 3行至最後一行
 * @author 洪政緯
 *
 */
public final class AppointedHaishinBusinessUnitConverter extends HaishinBusinessUnitConverter {
	private FormatParser appointedTerminalsParser;
	
	private AppointedHaishinBusinessUnit appointedHaishinBusinessUnit;
	
	// 指示端末清單:確認檔內容的第 2 行至最後一行
	private final int appointedTerminalFromLineNo = 3;
	
	public AppointedHaishinBusinessUnitConverter() {
		super();
	}
	
	@Override
	void parseBizTypeContent(HaishinBusinessUnit haishinBusinessUnit)
							throws WrongUnitFormatException{
		this.appointedHaishinBusinessUnit = 
				(AppointedHaishinBusinessUnit)haishinBusinessUnit;
		
		// 分析並塞入指示配信內容
		doFill();
	}
	
	/*
	 * 放入指示配信內容資訊:
	 * (1) 指示端末清單
	 */
	private void doFill() throws WrongUnitFormatException {
		// --------------------- (1) 指示端末清單 ---------------------
		// throws WrongConfirmationContentFormatException
		fillAppointedTerminalsList();
	}
	
	private void fillAppointedTerminalsList() 
						throws WrongUnitFormatException {
		try {
			/*
			 *  throws IllegalArgumentException
			 *  拋此例外，表示指定的 From 參數大於 End 的大小
			 *  (fromIndex > toIndex)
			 */
			String[] appointedTerminals = 
				super.getConfirmationContentFromLineToEnd(
								this.appointedTerminalFromLineNo - 1);
			this.appointedHaishinBusinessUnit.
							setAppointedTerminals((List<String>)this.appointedTerminalsParser.parse(appointedTerminals));
		} catch (IllegalArgumentException e) {
			throw new WrongUnitFormatException(
					"Can't find terminals from line " +
					this.appointedTerminalFromLineNo + " to end", e);
		}
	}
	
	// --- getter and setter ---

	public void setAppointedTerminalsParser(
			FormatParser appointedTerminalsParser) {
		this.appointedTerminalsParser = appointedTerminalsParser;
	}
}
